package Server;

import Server.Domain.Message;
import Server.Domain.MessageType;
import Server.Domain.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.TimerTask;

import static Server.ServerView.*;

public class Task extends Thread {
    Socket socket ;
    User user;
    ObjectInputStream ois;
    Message message;
    ObjectOutputStream oos;
    Boolean userOnline = true;
    @Override
    public void run() {
        while(true) {
            try {   //持续接受用户请求信息
                //头 - 对象
                ois = new ObjectInputStream(socket.getInputStream());
                message = (Message) ois.readObject();
            } catch (IOException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            //解析
            switch (message.getMessageType().getValue()) {
                case 3:
                    try{
                        oos = new ObjectOutputStream(socket.getOutputStream());
                        message.setContent(serverUserData.getUser());
                        oos.writeObject(message);
                        //头 - 对象
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case 8:
                 try{
                        oos = new ObjectOutputStream(socket.getOutputStream());
                        message.setContent(serverOnlineData.getOnline());
                        oos.writeObject(message);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }

                    break;
                case 4:
//                    System.out.println("私聊");
                    serverTaskData.getTask(message.getReceiver()).send(message);
                    System.out.println("发给"+message.getReceiver()+"的消息传达中..");
                    //传递给另一子线程
                    break;


                case 5:
                    for(Task task:serverTaskData.getTasks(message.getSender())){
                        System.out.println("发给"+task.user.getName()+"的消息传达中..");
                        task.send(message);
                    }
                    break;
                case 6:
                    System.out.println("转发文件");
                    break;
                case 7:
                    //下线的同时结束socket,设置状态为下线
                    userOnline = false;
                    serverOnlineData.offline(user);
                    try {
                        socket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
            }
            //返回
            if (!userOnline) {break;}

        }

    }
    public void send(Message message){
        try {
            System.out.println("消息传达中...");
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
            System.out.println("发送成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
